<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="btnaddBrand">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="格式" value="1"></el-option>
              <el-option label="分类" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 设备归属管理table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="130" align="center" prop="format" label="文件格式"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="tfmedia_id" label="分类ID"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="130" align="center" prop="name" label="分类"></el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getqueryIdTfMedia(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="getdeleteTfMedia(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 隐藏 -->
    <!-- 添加dialog表单 -->
    <div>
      <el-dialog title="编辑" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
        <el-form :model="addForm" ref="ruleForm" :rules="rules">
          <el-form-item label="文件格式:" label-width="100px" prop="addr">
            <el-input v-model="addForm.format" class="add_select" placeholder="请输入文件格式" maxlength="30"></el-input>
          </el-form-item>
          <el-form-item label="分类ID:" label-width="100px" prop="addr">
            <el-input v-model="addForm.tfmedia_id" class="add_select" placeholder="请输入分类ID(1图形,2音频,3视频,4文件)" maxlength="30"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
          <el-button v-if="editSubjects" type="primary" @click="getaddTfMedia('ruleForm')" size="mini" round>确 定</el-button>
          <el-button v-else type="primary" @click="geteditTfMedia('ruleForm')" size="mini" round>编辑</el-button>
        </div>
      </el-dialog>
      <!-- 删除弹窗 -->
      <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
    </div>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryTfMedia, queryIdTfMedia, editTfMedia, addTfMedia, deleteTfMedia } from "@/api/deployment.js";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["文件格式", "分类ID", "分类"],
      key: 1,
      colData: [], //动态控制table
      total: 0,
      searchTable: "",
      select: "", //搜索条件
      //添加按钮控制
      dialogFormVisible: false,
      //编辑按钮控制
      editSubjects: true,
      //浏览器专题id
      subjectId: 0,
      //归属table
      browserTable: [],
      //添加表单
      addForm: { format: "", tfmedia_id: "", },
      rules: { format: [{ required: true, message: "请输入文件格式", trigger: "blur" }], tfmedia_id: [{ required: true, message: "请输入分类ID", trigger: "blur" }] },
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), type: 1, format: undefined, tfmedia_id: undefined },
      //删除弹窗
      deleteDialogs: { show: false, deleteText: '', deleteId: 0 }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
    this.getqueryTfMedia()
  },
  methods: {
    //删除
    getdeleteTfMedia(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
      deleteTfMedia(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryTfMedia()
      })
    },
    //添加TF卡信息
    getaddTfMedia(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.last_ip = sessionStorage.getItem("Ip")
          addForm.userId = localStorage.getItem("ID")
          addTfMedia(addForm).then(res => {
            this.$message.success(res.msg)
            this.getqueryTfMedia()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false;
        }
      });
    },
    //编辑TF卡信息
    geteditTfMedia(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.last_ip = sessionStorage.getItem("Ip")
          addForm.userId = localStorage.getItem("ID")
          addForm.id = this.subjectId
          editTfMedia(addForm).then(res => {
            this.dialogFormVisible = false
            this.$message.success(res.msg)
            this.getqueryTfMedia()
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    //根据ID获取TF卡信息
    getqueryIdTfMedia(id) {
      this.editSubjects = false
      this.subjectId = id
      queryIdTfMedia({ id: id }).then(res => {
        this.dialogFormVisible = true
        let data = res.data
        this.addForm.format = data.format
        this.addForm.tfmedia_id = data.tfmedia_id
      }).catch(err => { })
    },
    getqueryTfMedia(startSize = 1, pageSize = 10, type = 1, parameter) {
      queryTfMedia(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
      }).catch(err => {
        this.browserTable = []
        this.total = 0
      })
    },
    //搜索,1:格式，2:分类
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.type = 2
          this.listQuery.format = this.searchTable
          this.getqueryTfMedia(1, 10, 2, { 'format': this.searchTable });
          break;
        case '2':
          this.listQuery.type = 3
          this.listQuery.tfmedia_id = this.searchTable
          this.getqueryTfMedia(1, 10, 3, { 'tfmedia_id': this.searchTable });
          break;
      }
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryTfMedia();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryTfMedia();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), type: 1, format: undefined, tfmedia_id: undefined }
      this.getqueryTfMedia()
    },
    btnaddBrand() {
      this.editSubjects = true
      this.dialogFormVisible = true
    },
    //编辑添加表单关闭事件
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = { format: "", tfmedia_id: "" }
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>